package com.orangelife.mobile.parkingfee.activity;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;

import com.curry.android.http.HttpUtil;
import com.curry.android.util.DialogHelper;
import com.curry.android.util.JSONHelper;
import com.curry.orangelife.mobile.R;
import com.markupartist.android.widget.PullToRefreshListView;
import com.markupartist.android.widget.PullToRefreshListView.IXListViewListener;
import com.orangelife.mobile.common.activity.OrangeLifeBaseActivity;
import com.orangelife.mobile.common.biz.JSONRequest;
import com.orangelife.mobile.constants.Constant;
import com.orangelife.mobile.parkingfee.adapter.ParkingFeeListViewAdapter;
import com.orangelife.mobile.util.BLog;
import com.orangelife.mobile.util.DateUtil;
import com.orangelife.mobile.util.JsonReponseHandler;
import com.orangelife.mobile.util.StringUtil;
import com.orangelife.mobile.util.ToastHelper;

public class ParkingFeeListActivity extends OrangeLifeBaseActivity {

	/** 标题、共计充值、上次充值时间 */
	private TextView tvTitle, tvTotalRecharge, tvLastTimeRecharge;
	/** 下拉刷新的listview */
	private PullToRefreshListView lvList;
	private Handler mHandler;
	/** 初始化页面的数据数量 */
	private int pageSize = Constant.PAGE_SIZE_DEFAULLT;
	/** 数据 */
	private List<Map<String, Object>> list, list2;
	private Map<String, Object> mapDetail;
	/** 初始化页数 */
	private int pageNo = Constant.PAGE_NO_DEFAULT;
	private ParkingFeeListViewAdapter<Map<String, Object>> adapter;
	/** 判断是否跳转到登录页，0：没有，1：有 */
	private int isTurnToLogin = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_property_bill);

		initeView();
		findView();
		getParkingFeeList(pageSize, pageNo);
		getParkingFeeTotal();
		list.clear();
		adapter.notifyDataSetChanged();
	}

	@Override
	protected void onResume() {
		super.onResume();
		closeActivity();
		BLog.getInstance().intAppmod(BLog.PARKING);
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		BLog.getInstance().outAppmod(BLog.PARKING);
	}

	public void hideInput(View v) {
		InputMethodManager imm = (InputMethodManager) this
				.getSystemService(Context.INPUT_METHOD_SERVICE);
		// edit.setCursorVisible(false);//失去光标
		imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
	}

	/** 初始化组件 */
	private void initeView() {
		tvTitle = (TextView) findViewById(R.id.tv_title);
		tvTotalRecharge = (TextView)findViewById(R.id.tvTotalRecharge);
		tvLastTimeRecharge = (TextView)findViewById(R.id.tvLastTimeRechargeTime);
		lvList = (PullToRefreshListView) findViewById(R.id.lv_property_bill);
	}

	/** 设置组件的值或状态 */
	private void findView() {
		tvTitle.setText("我的停车费");
		mHandler = new Handler();
		pullListView();
		setAdapter();
	}


	/**
	 * 下拉刷新列表
	 */
	private void pullListView() {
		lvList.setXListViewListener(new IXListViewListener() {
			@Override
			public void onRefresh() {
				mHandler.postDelayed(new Runnable() {

					@Override
					public void run() {
						list.clear();
						pageNo = 1;
						notifyPage();
					}
				}, 2000);
			}

			@Override
			public void onLoadMore() {
				mHandler.postDelayed(new Runnable() {
					@Override
					public void run() {
						pageNo += 1;
						notifyPage();
					}
				}, 2000);
			}
		});
	}

	/**
	 * 请求数据与更新界面
	 */
	private void notifyPage() {
		getParkingFeeList(pageSize, pageNo);
		onLoad();
		if (list != null) {
			setList();
		}
	}

	/**
	 * 更新界面
	 */
	private void setList() {
		adapter.setList(list);
		adapter.notifyDataSetChanged();
	}

	@SuppressWarnings("deprecation")
	private void onLoad() {
		lvList.stopRefresh();
		lvList.stopLoadMore();
		lvList.setRefreshTime(new Date().toLocaleString());
	}

	/**
	 * 刷新下拉列表
	 * 
	 * @param list2
	 */
	private void updateListView(List<Map<String, Object>> list2) {
		/** 判断list2是否为空 */
		if (list2 != null && list != null) {
			if (list2.size() == 0) {
				lvList.setPullLoadEnable(false);
				setList();
				DialogHelper.closeDialog(dialog);
			} else {
				lvList.setPullLoadEnable(true);
				list.addAll(list2);
				setList();
				DialogHelper.closeDialog(dialog);
			}
		} else {
			lvList.setPullLoadEnable(false);
		}
	}

	/**
	 * 设置我的物业费列表的apdater
	 */
	private void setAdapter() {
		list = new ArrayList<Map<String, Object>>();
		list2 = new ArrayList<Map<String, Object>>();
		adapter = new ParkingFeeListViewAdapter<Map<String, Object>>(
				this, list);
		lvList.setPullLoadEnable(false);
		lvList.setAdapter(adapter);
	}

	/**
	 * 获取我的停车费列表数据
	 */
	private void getParkingFeeList(int pageSize, int pageNo) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("pageIndex", pageNo);
		map.put("pageSize", pageSize);
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url", Constant.URL_PARKINGBILL);
		rootMap.put("wat", 0);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}

	/** 获取停车费订单列表金额统计 */
	private void getParkingFeeTotal() {
		Map<String, Object> map = new HashMap<String, Object>();
		Map<String, Object> rootMap = new HashMap<String, Object>();
		rootMap.put("map", map);
		rootMap.put("url",Constant.URL_PARKINGBILLTOTAL);
		rootMap.put("wat", 1);
		new JSONRequest(JSONRequest.GET, rootMap, handler, HttpUtil.ACTION_URI);
	}

	/** 接收数据判断是否添加成功 */
	@SuppressLint("HandlerLeak")
	Handler handler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				list2 = JsonReponseHandler.getListFromJson(
						String.valueOf(msg.obj), adapter, dialog);
				updateListView(list2);
				break;
			case 1:
				mapDetail = new HashMap<String, Object>();
				mapDetail = (Map<String, Object>) JSONHelper.jsonToMap(
						msg.obj.toString()).get("entity");
				DateUtil dateUtil = new DateUtil();
				String time = dateUtil.tranStringForDate(mapDetail.get("lasttime").toString());
				String countCost = mapDetail.get("countCost").toString();
				if(StringUtil.isBlank(countCost)){
					tvTotalRecharge.setText("0.00");
				}else{
					tvTotalRecharge.setText(mapDetail.get("countCost").toString());
				}
				if(StringUtil.isBlank(time)){
					tvLastTimeRecharge.setText("暂无");
				}else{
					tvLastTimeRecharge.setText(time);
				}
				break;
			case 1011:
				if(isTurnToLogin == 0) {
					ToastHelper.getInstance()._toast(msg.obj.toString());
					isLogin(dialog);
					isTurnToLogin = 1;
				}
				break;

			default:
				break;
			}
		}

	};

}
